﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Office.Interop.Word;

namespace MWTools.Client
{
    public class WordController : ControllerBase, IDisposable
    {
        public static string ConvertWord2Html(Document document)
        {
            string originalFileName = document.FullName;
            Application app = document.Application;

            if (!System.IO.Directory.Exists(@"C:\TEMP"))
                System.IO.Directory.CreateDirectory(@"C:\Temp");

            string tempFileName = String.Format(@"C:\temp\{0}.doc", System.Guid.NewGuid().ToString("n"));

            // Constant for WORD-TO-HTML exporting format
            const int WORD_HTML_FORMAT = 8;

            document.SaveAs(tempFileName,
                WORD_HTML_FORMAT,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                false,
                Type.Missing,
                Type.Missing);
            document.Close();
            app.Documents.Open(originalFileName, Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing,
                Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
            
            using (System.IO.FileStream fs = new System.IO.FileStream(tempFileName,
                                                 System.IO.FileMode.Open,
                                                 System.Security.AccessControl.FileSystemRights.ReadData,
                                                 System.IO.FileShare.Read,
                                                 4096, System.IO.FileOptions.RandomAccess))
            {
                using (System.IO.StreamReader sr = new System.IO.StreamReader(fs, Encoding.Default))
                {
                    string content = sr.ReadToEnd();
                    return content;
                }
            }


        }

        public override void SubmitItem(object item)
        {
            Document docToSubmit = (Document)item;


            string revisedConent = string.Empty;
            string fileName = docToSubmit.FullName;
            string content = ConvertWord2Html(docToSubmit);

            PandocWrapper.PandocWrapper wrapper = new PandocWrapper.PandocWrapper();
            revisedConent = wrapper.Convert(content, "html", "mediawiki");
            string newContent = String.Empty;

            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {

                using (System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(ms, UTF8Encoding.UTF8))
                {
                    writer.WriteStartElement("wordItem");
                    writer.WriteElementString("title", fileName);
                    writer.WriteElementString("htmlBody", revisedConent);
                    writer.WriteEndElement();
                    writer.Flush();
                    writer.BaseStream.Seek(0, System.IO.SeekOrigin.Begin);

                    using (System.IO.StreamReader sr = new System.IO.StreamReader(writer.BaseStream))
                    {
                        newContent = sr.ReadToEnd();


                    }

                }

                Helpers.PluginHelpers.SubmitItemToPlugin(System.Guid.NewGuid().ToString("n"), newContent);

            }
        }

        public void Dispose()
        {

        }
    }
}
